import goodSpecs from "components/goods/children/goodSpecs"; //规格弹窗
import yzWechatShareImg from "components/ui_components/yz_wechatShareImg";
import yzSpecs from "components/ui_components/yz_specs";
import yzService from "components/ui_components/yz_service.vue";
import yzGoodsPopup from "components/ui_components/yz_goodsPopup";
import floatOrder from "components/goods/children/float_order";
import { Toast } from "vant";
import base64_encode from "../../utils/base64_encode";

var specsManage = []; //选择池 用于排序
var optionsId = ""; //选择后的 规格ID

export default {
  components: { yzWechatShareImg, yzService, yzGoodsPopup, goodSpecs, yzSpecs, floatOrder },
  data() {
    return {
      radioChoose: "",
      popupSpecs: false,
      showgp: false,

      serviceShow: false,
      showWechatshar: false,
      message_card: "",
      goodsCartsTotal: [],
      index_goods: 0, //当前商品索引
      activity_gid: null, //上一次点击查看详情的商品id

      list: [],
      carts: [],
      goodsInfo: {},
      nums: 0,
      sn: "",
      show: false,
      isLoadMore: false,
      isLogin: false,
      showGood: false,
      goods_num: 0,
      Height: 0,
      share: 0,
      login_type:"",

      sn_code: "", //兑换码序列号
      code_id: "", //兑换码id
      article_url: "",

      show2: false,
      popThumb: "",
      popNum: 0,
      popTitle: "",
      popPrice: "",
      specid: "",

      has_many_options: [],
      has_many_goods: [], //多规格商品

      showChoose: false,
      dispatch_types: [],
      current_index: 0,

      details: {},
      last_page: 1,
      current_page: 1
    };
  },
  activated() {
    this.init();
    this.article_url = window.location.href + "&show=true";
    if (this.$route.query.code_id || this.$route.query.sn_code) {
      this.sn_code = this.$route.query.sn_code || "";
      this.code_id = this.$route.query.code_id || "";
      localStorage.setItem("exchange-code", JSON.stringify({ sn_code: this.$route.query.sn_code, code_id: this.$route.query.code_id }));
    } else {
      let { sn_code, code_id } = JSON.parse(localStorage.getItem("exchange-code"));
      this.sn_code = sn_code || "";
      this.code_id = code_id || "";
    }
    this.getData(this.code_id, this.sn_code);
  },
  methods: {
    changeCount(num, index) {
      if (index == 1) {
        this.popNum = num;
        this.has_many_goods.forEach((item, i) => {
          if (item.option_id == optionsId) {
            this.$set(this.has_many_goods, i, { ...item, total: num });
          }
        });
      } else {
        if (this.popNum == 0) {
          this.popNum = 0;
          this.has_many_goods.forEach((item, i) => {
            if (item.option_id == optionsId) {
              this.$set(this.has_many_goods, i, { ...item, total: 0 });
            }
          });
        } else {
          this.popNum = this.popNum - 1;
          this.has_many_goods.forEach((item, i) => {
            if (item.option_id == optionsId) {
              this.$set(this.has_many_goods, i, { ...item, total: num });
            }
          });
        }
      }
      this.updateNums();
    },
    //更新兑换数量
    updateNums() {
      let nums1 = 0;
      if (this.has_many_goods && this.has_many_goods.length !== 0) {
        this.has_many_goods.forEach(item => {
          if (item.goods_id == this.list[this.current_index].id) {
            nums1 += item.total;
          }
        });
        this.list[this.current_index].count = nums1;
      }

      let nums = 0;
      this.list.forEach(item => {
        nums += item.count;
      });
      this.nums = nums;
      if (this.nums > this.details.exchange_limit) {
        this.$toast("选择的数量已经超过兑换数量限制，请取消后再重新选择！");
        return;
      }
    },
    //多规格购物车
    submitAction() {
      if (specsManage && specsManage.length !== 0 && specsManage.length < this.goodsInfo.has_many_specs.length) {
        //请选择规格
        // this.show2 = false;
        Toast(this.goodsDescription);
        return;
      }

      let count = this.popNum + 1;
      this.popNum = count;
      console.log(this.has_many_goods, "this.has_many_goods===");
      console.log(this.list[this.current_index].id, "当前商品id");
      this.has_many_goods.push({ option_id: optionsId, goods_id: this.list[this.current_index].id, total: 1 });

      if (this.details.single_order == 1) {
        //单选
        if (this.has_many_goods && this.has_many_goods.length !== 0) {
          let _arr = this.has_many_goods.filter(item => {
            if (item.goods_id == this.list[this.current_index].id) {
              return item;
            }
          });
          this.has_many_goods = _arr;
        }

        this.list.forEach(item => {
          if (item.id !== this.list[this.current_index].id) {
            item.count = 0;
          }
        });
      } else {
        this.list.forEach(item => {
          if (item.id === this.list[this.current_index].id) {
            item.count = 1;
          }
        });
      }
      this.updateNums();
    },

    //界面选择规格触发事件
    selectSpecs(data, id) {
      //处理选择池
      this.manageSpecs(data);
      //处理规格组合选择状态
      this.setGoodsSpecs(data);
      //设置选择规格后的 价格、照片、库存
      this.setGoodsSpecsChangeInfo(id);
    },

    //设置选择规格后的 价格、照片、库存
    setGoodsSpecsChangeInfo(id) {
      //根据ID 排序 specsManage.sort();
      specsManage.sort(function(a, b) {
        return a.id - b.id;
      });
      if (specsManage.length === this.goodsInfo.has_many_specs.length) {
        let goodsSpecs = "";
        for (let j = 0; j < specsManage.length; j++) {
          goodsSpecs += specsManage[j].id + "_";
        }
        goodsSpecs = goodsSpecs.substring(0, goodsSpecs.length - 1); //处理"_"
        this.specid = goodsSpecs;
        console.log(this.specid, "this.specid-188");
        for (let i = 0; i < this.goodsInfo.has_many_options.length; i++) {
          if (goodsSpecs === this.setGoodsSpecsBySort(this.goodsInfo.has_many_options[i].specs)) {
            this.popmPrice = this.goodsInfo.has_many_options[i].market_price; //设置价格
            if (this.goodsInfo.point_goods) {
              this.popPrice = this.goodsInfo.point_goods.point_option[i].goods_point; //设置价格
            } else {
              this.popPrice = this.goodsInfo.has_many_options[i].product_price; //设置价格
            }
            this.popThumb = this.fun.isTextEmpty(this.goodsInfo.has_many_options[i].thumb) ? this.goodsInfo.thumb : this.goodsInfo.has_many_options[i].thumb; //设置图片
            optionsId = this.goodsInfo.has_many_options[i].id; //设置规格ID，用于生成订单
            break;
          }
        }
      } else {
        let goodsSpecs = "";
        for (let j = 0; j < specsManage.length; j++) {
          goodsSpecs += specsManage[j].id + "_";
        }
        this.specid = goodsSpecs;
      }

      let _goods = this.has_many_goods.filter(item => item.option_id == optionsId);
      if (_goods.length == 0) {
        this.popNum = 0;
      } else {
        this.popNum = _goods[0].total;
      }
    },
    //处理规格组合选择状态 过滤数据
    setGoodsSpecs(specs) {
      for (let i = 0; i < this.goodsInfo.has_many_specs.length; i++) {
        if (specs.specid != this.goodsInfo.has_many_specs[i].id) {
          this.setGoodsSpecsStatus(this.goodsInfo.has_many_specs[i].specitem, specs.id);
        }
      }
    },
    //处理商品goodsSpecs 并排序 新方法
    setGoodsSpecsBySort(specs) {
      let _specs = specs.split("_"); //先变成数组
      //_specs.sort();//排序
      _specs.sort(function(a, b) {
        return a - b;
      });

      // 在组装回去
      let goodsSpecs = "";
      for (let j = 0; j < _specs.length; j++) {
        goodsSpecs += _specs[j] + "_";
      }
      goodsSpecs = goodsSpecs.substring(0, goodsSpecs.length - 1); //处理"_"

      return goodsSpecs;
    },
    //处理规格组合选择状态 处理状态 specitem 组合数组(未选中的) id当前选中的ID 根据ID 组合算是否有当前组合
    setGoodsSpecsStatus(specitem, id) {
      let options = []; //数据池

      for (let i = 0; i < this.goodsInfo.has_many_options.length; i++) {
        let _specs = this.goodsInfo.has_many_options[i].specs.split("_");
        //判断是否包含
        for (let j = 0; j < _specs.length; j++) {
          if (_specs[j] == id) {
            options.push(this.goodsInfo.has_many_options[i]);
          }
        }
      }

      for (let m = 0; m < options.length; m++) {
        let _specs = options[m].specs.split("_");
        for (let y = 0; y < _specs.length; y++) {
          if (_specs[y] != id && options[m].stock == 0) {
            for (let n = 0; n < specitem.length; n++) {
              if (_specs[y] == specitem[n].id) {
                specitem[n].c = true;
              }
            }
          } else if (_specs[y] != id && options[m].stock > 0) {
            for (let n = 0; n < specitem.length; n++) {
              if (_specs[y] == specitem[n].id) {
                specitem[n].c = false;
              }
            }
          }
        }
      }
    },
    //处理选择池
    manageSpecs(data) {
      var specsObject = new Object();
      specsObject.id = data.id;
      specsObject.specid = data.specid;
      specsObject.title = data.title;
      if (specsManage.length > 0) {
        for (let i = 0; i < specsManage.length; i++) {
          if (specsManage[i].specid == specsObject.specid) {
            specsManage.splice(i, 1);
          }
        }
        specsManage.push(specsObject);
      } else {
        specsManage.push(specsObject);
      }
      console.log(specsManage.length == this.goodsInfo.has_many_specs.length, specsManage, "specsManage==");
      //排序
      if (specsManage.length == this.goodsInfo.has_many_specs.length) {
        var newSpecsManage = [];
        for (let i = 0; i < this.goodsInfo.has_many_specs.length; i++) {
          for (let j = 0; j < specsManage.length; j++) {
            if (this.goodsInfo.has_many_specs[i].id == specsManage[j].specid) {
              newSpecsManage.push(specsManage[j]);
              break;
            }
          }
        }
        specsManage = newSpecsManage;
      }
      console.log(specsManage, "specsManage");
      this.setGoodsDescription();
    },

    //处理goodsDescription 数据
    setGoodsDescription() {
      var description = "";
      //相等代表全选了 体现语句
      if (specsManage.length == this.goodsInfo.has_many_specs.length) {
        description = "已选择 ";
        for (let i = 0; i < specsManage.length; i++) {
          description += specsManage[i].title + " ";
        }
        this.goodsDescription = description;
      } else {
        description = "请选择 ";

        for (let i = 0; i < this.goodsInfo.has_many_specs.length; i++) {
          for (let j = 0; j < specsManage.length; j++) {
            if (this.goodsInfo.has_many_specs[i].id != specsManage[j].specid) {
              description += this.goodsInfo.has_many_specs[i].title + " ";
              break;
            }
          }
        }
        this.goodsDescription = description;
      }
    },
    showgoodspecs() {
      this.list.forEach((item, i) => {
        if (item.id == this.activity_gid) {
          if (item.has_option == 1) {
            this.showgp = false;
            this.show2 = true;
            this.selectOption(item.id, i, item);
          } else {
            this.popupSpecs = true;
          }
        }
      });
    },
    //选规格
    selectOption(id, i, item) {
      optionsId = "";
      specsManage = [];
      this.specid = "";
      this.popNum = 0;
      this.goodsInfo = {};

      this.current_index = i;
      this.activity_gid = id;
      this.show2 = true;
      this.popThumb = item.thumb;
      this.popTitle = item.title;
      this.popPrice = item.price;
      $http.get("goods.goods.get-goods-page", { id: Number(id) }, ".").then(res => {
        if (res.result == 1) {
          this.goodsInfo = res.data.get_goods;
          if (!this.specid.length) {
            if (this.goodsInfo.point_goods) {
              this.popPrice = this.goodsInfo.point_goods.min_point + "-" + this.goodsInfo.point_goods.max_point; //设置默认价格
            } else {
              this.popPrice = this.goodsInfo.min_price + "-" + this.goodsInfo.max_price; //设置默认价格
            }
          }
          this.goodsDescription = "请选择";
          for (let i = 0; i < this.goodsInfo.has_many_specs.length; i++) {
            this.goodsDescription += " " + this.goodsInfo.has_many_specs[i].title;
          }
          if (!optionsId) {
            // 默认选择第一个
            for (let i = 0; i < this.goodsInfo.has_many_specs.length; i++) {
              if (this.goodsInfo.goods_option_ids && this.goodsInfo.goods_option_ids.length >= 0) {
                for (let j = 0; j < this.goodsInfo.has_many_specs[i].specitem.length; j++) {
                  if (this.goodsInfo.has_many_specs[i].specitem[j].id == Number(this.goodsInfo.goods_option_ids[i])) {
                    this.selectSpecs(this.goodsInfo.has_many_specs[i].specitem[j], this.goodsInfo.has_many_specs[i].specitem[j].id);
                  }
                }
              } else {
                this.selectSpecs(this.goodsInfo.has_many_specs[i].specitem[0], this.goodsInfo.has_many_specs[i].specitem[0].id);
              }
            }
          }
        }
      });

      // this.showGoodPup(id, true, item, i);
    },
    //多规格商品-默认规格
   async get_specification(id){
    return new Promise(async (resolve,reject)=>{
      let {result,data} = await  $http.get("goods.goods.get-goods-page",{id:Number(id)},"");
      if(result){
        if(data.get_goods.has_many_options && data.get_goods.has_many_options.length!==0){
          resolve(data.get_goods.has_many_options[0].id)
        }else{
          resolve(0)
        }
      }else{
        reject()
      }
    })

    },


    showGoodPup(_id, status, _item, i) {
      if(this.details.show_detail==0){
        return;
      }
      this.index_goods = i;
      if (this.carts && this.carts.length !== 0) {
        this.carts.forEach(item => {
          if (item.id !== _id) {
            this.carts.push({ ..._item, total: this.list[i].count, goods_id: _item.id });
          }
        });
      } else {
        this.carts.push({ ..._item, total: this.list[i].count, goods_id: _item.id });
      }
      if (this.activity_gid == _id) {
        if (!status) {
          this.showgp = true;
        } else {
          this.popupSpecs = true;
        }
        return;
      }
      this.activity_gid = _id;
      $http
        .get("goods.goods.get-goods-page", { id: Number(_id) }, ".")
        .then(res => {
          if (res.result == 1) {
            this.goodsInfo = res.data.get_goods;
            if (res.data.get_goods.content) {
              let str = this.goodsInfo.content.replace(/src=\"http:/gi, 'src="https:');
              this.goodsInfo.content = str;
            } else {
              this.goodsInfo.content = "暂无该商品详情";
            }
            setTimeout(() => {
              if (!status) {
                this.showgp = true;
              } else {
                this.popupSpecs = true;
              }
            }, 300);
          } else {
            Toast(res.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    async showPop(index, has_option, id) {
      if (has_option == 1) {
        this.index_goods = index;
        if (has_option == 1) {
          this.showgp = true;
          let { result, data, msg } = await $http.get("goods.goods.get-goods-page", { id });
          if (result) {
            this.goodsInfo = data.get_goods;
            setTimeout(() => {
              this.popupSpecs = true;
            }, 400);
          } else {
            this.$toast(msg);
          }
        }
      }
    },
    async binding() {
      let { result, msg, data } = await $http.get("plugin.exchange-code.api.record.bind", { sn: this.sn });
      this.$toast(msg);
      if (result) {
        this.sn = "";
        this.code_id = data.code_id;
        this.sn_code = data.sn;
        this.getData(data.code_id, data.sn);
      }
    },
    async getData(code_id, sn_code) {
      let { result, msg, data } = await $http.get("plugin.exchange-code.api.list.info", { id: code_id, scope: "pass", sn: sn_code }, "loading");
      if (result == 0) {
        return this.$toast(msg);
      }
      this.login_type = data.login_type;
      this.details = data;

      if (data.login_status == 0) {
        this.show = true;
        return;
      }
      if (data.type == 1) {
        this.sn_code = data.current_code;
        this.getGoods({ id: data.id });
      }
      if (data.type == 2) {
        this.sn = data.current_code || this.$route.query.sn_code;
      }
      if (this.share == 0) {
        this.$nextTick(() => {
          this.fun.wxShare(
            "",
            {},
            {
              title: data.share_title,
              imgUrl: data.share_icon,
              description: data.share_desc
            }
          );
        });
      }
    },
    async getGoods(json) {
      let { result, msg, data } = await $http.get("plugin.exchange-code.api.list.goodList", json, "loading");
      if (result == 0) {
        return this.$toast(msg);
      }
      this.current_page = data.current_page;
      this.last_page = data.last_page;

      if (data.current_page == 1) {
        this.list = data.data;
        this.list.forEach(item => {
          item.count = 0;
        });
        this.element_height();
      } else {
        let arr = data.data;
        arr.forEach(item => {
          item.count = 0;
        });
        this.list = this.list.concat(arr);
      }
    },
    getMoreData() {
      this.getGoods({ page: this.current_page + 1, id: this.code_id });
    },
    close_yz_specs_popup(_optionsId, goodsCount, clicktype, id) {
      this.popupSpecs = false;
      if (clicktype == 1) {
        this.carts.forEach(item => {
          if (item.id == this.activity_gid) {
            item.total = goodsCount;
            item.option_id = _optionsId;
          } else {
            item.total = 0;
            item.option_id = "0";
          }
        });
        this.list.forEach(item => {
          if (item.id == this.activity_gid) {
            item.option_id = _optionsId;
          } else if (item.has_option == 0) {
            item.option_id = "0";
          }
        });
        this.list.forEach((list_item, i) => {
          if (this.details.single_order == 1) {
            //单选
            if (this.activity_gid == list_item.id) {
              this.$set(this.list, i, { ...this.list[i], count: goodsCount });
            } else {
              this.$set(this.list, i, { ...this.list[i], count: 0 });
            }
          } else {
            if (this.activity_gid == list_item.id) {
              this.$set(this.list, i, { ...this.list[i], count: goodsCount });
            }
          }
        });
        let nums = 0;
        this.list.forEach(item_2 => {
          nums += item_2.count;
        });
        this.nums = nums;
      }
    },
    video_handle() {
      this.$toast("兑换数量最多为1");
    },
    share_handle() {
      let { title, imgUrl, description } = JSON.parse(localStorage.getItem("wxShareInfo"));
      if (title || imgUrl || description) {
        console.log("title", title, "imgUlr", imgUrl, "description", description);
        this.fun.wxShare("", {}, { title, imgUrl, description });
      }
      this.showWechatshar = true;
    },
    element_height() {
      //商品组元素高度
      if (this.list && this.list.length !== 0) {
        this.$nextTick(() => {
          if (this.$refs.box.length > 6) {
            //元素大于5个固定高度，局部区域滑动显示商品
            let h = 0;
            this.$refs.box.forEach((item, index) => {
              this.goods_num += 1;
              if (index <= 5) {
                h += item.clientHeight;
              }
            });
            this.Height = h;
          }
        });
      }
    },
    init() {
      this.login_type = "";
      this.showChoose = false;
      this.code_id = "";
      this.nums = 0;
      this.popNum = 0;
      this.share = 0;
      this.has_many_goods = [];
      this.details = {};
      this.isLogin = false;
      this.isLoadMore = false;
      if (this.$route.params.share) {
        this.share = Number(this.$route.params.share);
        if (this.$route.query.show) {
          return;
        }
        this.show = true;
      }
      let shopName = this.$store.state?.temp?.mailInfo?.name || "";
      if (shopName) {
        this.fun.setWXTitle(shopName);
      } else {
        let yz_basic_info = JSON.parse(localStorage.getItem("yz_basic_info"));
        if (yz_basic_info) {
          this.fun.setWXTitle(yz_basic_info.home?.mailInfo?.name);
        }
      }
    },
    operation_handle(i, nums, type) {
      this.index_goods = i;
      if (type == "minus") {
        if (nums == 1) {
          this.list[i].count = 0;
        } else {
          this.list[i].count -= 1;
        }
        this.nums -= 1;
      } else {
        this.nums += 1;
        this.list[i].count += 1;
      }
      if (this.nums > this.details.exchange_limit) {
        this.$toast("选择的数量已经超过兑换数量限制，请取消后再重新选择！");
      }
    },
    async cart_handle(index, item) {
      optionsId = "";
     let option_id =  null;
     if(item.has_option !== 2){
      option_id =  await this.get_specification(item.id);
     }else{
      option_id = 0;
     }
     console.log(option_id,"options_id===");
      this.index_goods = index;
      if (this.details.single_order == 1) {
        this.has_many_goods = [];
        this.specid = "";
      }
      this.list.forEach((list_item, i) => {
        if (this.details.single_order == 1) {
          //单选
          if (item.id == list_item.id) {
            this.$set(this.list, i, { ...list_item, count: 1,option_id });
          } else {
            this.$set(this.list, i, { ...list_item, count: 0,option_id });
          }
        } else {
          if (item.id == list_item.id) {
            this.$set(this.list, i, { ...list_item, count: 1,option_id });
          }
        }
      });
      this.updateNums();
    },

    link_enrollEnter(exchange_code_id) {
      let _arr = this.list.filter(item => {
        if (item.count >= 1) {
          return item;
        }
      });

      this.$router.push(
        this.fun.getUrl("enrollEnter", {
          id: _arr[0].relation_id,
          exchange_code_id
        })
      );
    },
    async check() {
      let arr = [],
        plugins_ids = [];
      console.log(this.list,"list====");
      this.list.forEach(item => {
        if (item.count >= 1) {
          // let option_id = "0";
          if (item.has_option !== 1) {
            arr.push({ goods_id: item.id, total: item.count, option_id: item.option_id });
          }
          if (!plugins_ids.includes(item.plugin_id)) {
            plugins_ids.push(item.plugin_id);
          }
        }
      });
      if (this.has_many_goods && this.has_many_goods.length !== 0) {
        console.log("多规格购买");
        this.has_many_goods.forEach(item => {
          if (item.total >= 1) {
            arr.push({ goods_id: item.goods_id, total: item.total, option_id: item.option_id });
          }
        });
      }
      let ids = [];
      ids = arr.map(item => {
        return item.goods_id;
      });

      let { result, msg, data } = await $http.post("plugin.exchange-code.api.record.exchangeCheck", { sn: this.sn_code || this.$route.query.sn_code, goods: JSON.stringify(arr) }, "loading");
      if (result) {
        this.getLog(ids, arr, plugins_ids);
      } else {
        if (data && data.length !== 0) {
          if (data.need_choose == 1) {
            this.dispatch_types = data.dispatch_types;
            this.showChoose = true;
          } else {
            this.submitGoods();
          }
        } else {
          this.$toast(msg);
        }
      }
    },
    toBuy() {
      this.submitGoods();
    },
    submitGoods() {
      let goodsId = [],
        goods = [];
      if (this.showChoose && !this.radioChoose) {
        Toast("请先选择配送方式");
        return;
      }

      if (this.showChoose) {
        console.log(this.dispatch_types[this.radioChoose]);
        // 配送方式有冲突
        for (let i = 0; i < this.dispatch_types[this.radioChoose].member_carts.length; i++) {
          goodsId.push(this.dispatch_types[this.radioChoose].member_carts[i].goods_id);
          goods.push({
            total: this.dispatch_types[this.radioChoose].member_carts[i].total,
            goods_id: this.dispatch_types[this.radioChoose].member_carts[i].goods_id,
            option_id: this.dispatch_types[this.radioChoose].member_carts[i].option_id
          });
        }
      }
      goods = JSON.stringify(goods);
      $http.get("plugin.exchange-code.api.log.create", { id: this.sn_code }, "loading").then(res => {
        let {
          result,
          msg,
          data: { exchange_code_id }
        } = res;
        if (result) {
          this.$router.push(
            this.fun.getUrl(
              "goodsorder",
              {},
              {
                name: this.details.name,
                current_code: this.details.code,
                exchange_code_id,
                tag: "exchange_code",
                goods,
                goodsId: goodsId.join(",")
              }
            )
          );
        } else {
          this.$toast(msg);
        }
      });
    },
    // 立即兑换
    async redeem() {
      if (this.details.type == 2) {
        let { result, msg } = await $http.get("plugin.exchange-code.api.record.levelExchange", { id: this.sn }, "loading");
        if (result) {
          this.message_card = msg;
          this.show = true;
          this.sn = "";
        } else {
          this.$toast(msg);
        }
      } else {
        this.check();
      }
    },
    // 获取兑换码日志
    getLog(goodsId, arr, plugins_ids) {
      $http.get("plugin.exchange-code.api.log.create", { id: this.sn_code }, "loading").then(({ result, msg, data: { exchange_code_id } }) => {
        if (result) {
          if (plugins_ids.includes(46)) {
            //活动报名商品
            this.link_enrollEnter(exchange_code_id);
          }
          if (plugins_ids.includes(113)) {
            //多门店项目核销商品
            let total = this.goods_total();
            let json =  {
              tag: "store_projects_goods",
              root_tag: "store_projects_goods",
              total,
              optionsId,
              goodsId: goodsId[0],
              exchange_code_id,
              name: this.details.name,
              current_code: this.details.code,
              show_info:this.details.show_info
            }
            if(this.details.show_info==0){
              delete json.name;
              delete json.current_code
            }
            this.$router.push(
              this.fun.getUrl("goodsorder", {},json)
            );
          }
          if (plugins_ids.includes(140)) { //卡券商品
            let total = this.goods_total();
            let json = {
              tag: "-2",
              root_tag: "exchange_code",
              goodsId: goodsId[0],
              optionsId:arr[0].option_id,
              total,
              exchange_code_id,
              name: this.details.name,
              current_code: this.details.code,
              show_info:this.details.show_info
            };
            if(this.details.show_info==0){
              delete json.name;
              delete json.current_code
            }
            this.$router.push(this.fun.getUrl("goodsorder",{},json));
          }
          if (plugins_ids.includes(101)) {
            //门店预约商品
            let total = this.goods_total();
            let json ={
              tag: "appointment_goods",
              root_tag: "appointment_goods",
              goodsId: goodsId[0],
              exchange_code_id,
              optionsId,
              total,
              name: this.details.name,
              current_code: this.details.code,
              show_info:this.details.show_info
            };
            if(this.details.show_info==0){
                  delete json.name;
                  delete json.current_code
              }
            this.$router.push(this.fun.getUrl("goodsorder",{},json));
          }
          if (plugins_ids.includes(144)) {
            //消费券联盟商品
            let json = {
              goodsId: goodsId[0],
              exchange_code_id,
              total: arr[0].total,
              optionsId: arr[0].option_id || "0"
            };
            this.$router.push(this.fun.getUrl("orderAffirm", {}, json));
          }

          if (plugins_ids.includes(0) || plugins_ids.includes(120) || plugins_ids.includes(44) || plugins_ids.includes(103)) {
            //跳转到下单页
            this.link_order(arr, goodsId, exchange_code_id);
          }
        } else {
          this.$toast(msg);
        }
      });
    },
    // 商品数量
    goods_total() {
      let total = 0;
      this.list.forEach(item => {
        if (item.count >= 1) {
          total += item.count;
        }
      });
      return total;
    },
    link_order(goods, goodsId, exchange_code_id) {
      let json = {
        tag: "exchange_code",
        root_tag: "exchange_code",
        goodsId,
        name: this.details.name,
        current_code: this.details.code,
        exchange_code_id,
        goods: JSON.stringify(goods)
      };
      if(this.details.show_info==0){
          delete json.name;
          delete json.current_code
      }
      this.$router.push(
        this.fun.getUrl("goodsorder",{},json)
      );
    },
    // 复制链接
    onCopy() {
      this.show = false;
      Toast({
        message: "复制成功",
        position: "bottom",
        duration: 2000
      });
      this.article_url = "";
    },
    loginHandle() {
      console.log(this.fun.getTyep());
      if (this.fun.getTyep() == 5) {
        //h5
        sessionStorage.setItem("yz_redirect", base64_encode(document.location.href));
        this.$router.push(this.fun.getUrl("login", {}, { yz_redirect: base64_encode(document.location.href) }));
      } else if (this.fun.getTyep() == 1) {
        //微信端
        if(this.login_type == 'h5'){
          sessionStorage.setItem("yz_redirect", base64_encode(document.location.href));
          this.$router.push(this.fun.getUrl("login", {}, { yz_redirect: base64_encode(document.location.href) }));
        }else{
          let url = this.fun.getSiteRoot() + this.fun.getRealUrl("member.login.index", { yz_redirect: base64_encode(document.location.href) });
          window.location.href = url;
        }
      }
    }
  }
};
